package com.pingao.mp.modular.sys.controller;

import com.pingao.mp.modular.sys.entity.Log;
import com.pingao.mp.modular.sys.service.LogService;
import com.pingao.mp.utils.Constant;
import com.pingao.mp.utils.set.MapBuilder;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 创建人：wenpingao
 * 创建时间：2021/8/12 10:04
 * 备注：这是mp一个控制器
 **/
@Controller
@RequestMapping("/sys/log")
public class LogController {
    @Autowired
    LogService baseService;

    @GetMapping(value = "/index")
    @RequiresPermissions("sys.log.index")
    public String index() {
        return "sys/logg/index";
    }

    @GetMapping("/list")
    @ResponseBody
    @RequiresPermissions("sys.log.index")
    public Object list(@RequestParam("pageNumber") Integer pageNumber,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("searchText") String searchText,
                       @RequestParam("sortName") String sortName,
                       @RequestParam("sortOrder") String sortOrder) {
        return baseService.getList(pageNumber, pageSize, searchText, sortName, sortOrder);
    }

    @GetMapping("/add")
    @RequiresPermissions("sys.log.add")
    public String add() {
        return "sys/logg/add";
    }

    @PostMapping("/add")
    @RequiresPermissions("sys.log.add")
    @ResponseBody
    public Object add(Log entity) {
        MapBuilder<String, Object> map = new MapBuilder<>();
        if (!baseService.save(entity)) {
            map.put(Constant.ApiStatus, false).put(Constant.ApiMsg, "保存角色出错......");
            return map.get();
        }
        map.put(Constant.ApiStatus, true).put(Constant.ApiMsg, "保存角色成功").put(Constant.ApiData, entity);
        return map.get();
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("sys.log.edit")
    public String edit(@PathVariable("id") Integer id, ModelMap map) {
        Log entity = baseService.getById(id);
        if (entity == null) {
            throw new NullPointerException("该角色不存在--this role is not exits!!!");
        }
        map.addAttribute("entity", entity);
        return "sys/logg/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    @RequiresPermissions("sys.log.edit")
    public Object edit(Log entity) {
        MapBuilder<String, Object> map = new MapBuilder<>();
        baseService.updateById(entity);
        map.put(Constant.ApiStatus, true).put(Constant.ApiMsg, "保存角色成功").put(Constant.ApiData, entity);
        return map.get();
    }

    @PostMapping("/delete")
    @RequiresPermissions("sys.log.delete")
    @ResponseBody
    public Object delete(
            @RequestParam(value = "ids[]") List<Long> ids
    ) {
        MapBuilder<String, Object> map = new MapBuilder<>();
        baseService.removeByIds(ids);
        map.put(Constant.ApiStatus, true).put(Constant.ApiMsg, "删除成功").put(Constant.ApiData, ids);
        return map.get();
    }
}

//1425648207630962690
//1425648209484845057